home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / vb4comms / commcfg.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-01-25  |  6.4 KB  |  204 lines

  1. VERSION 4.00
  2. Begin VB.Form Commcfg 
  3.    Caption         =   "CommDemo Configuration"
  4.    ClientHeight    =   2715
  5.    ClientLeft      =   1515
  6.    ClientTop       =   1920
  7.    ClientWidth     =   4905
  8.    BeginProperty Font 
  9.       name            =   "MS Sans Serif"
  10.       charset         =   0
  11.       weight          =   700
  12.       size            =   8.25
  13.       underline       =   0   'False
  14.       italic          =   0   'False
  15.       strikethrough   =   0   'False
  16.    EndProperty
  17.    ForeColor       =   &H80000008&
  18.    Height          =   3120
  19.    Left            =   1455
  20.    LinkMode        =   1  'Source
  21.    LinkTopic       =   "Form1"
  22.    ScaleHeight     =   2715
  23.    ScaleWidth      =   4905
  24.    Top             =   1575
  25.    Width           =   5025
  26.    Begin VB.CommandButton CmdOk 
  27.       Appearance      =   0  'Flat
  28.       BackColor       =   &H80000005&
  29.       Caption         =   "Ok"
  30.       Height          =   495
  31.       Left            =   3960
  32.       TabIndex        =   5
  33.       Top             =   360
  34.       Width           =   735
  35.    End
  36.    Begin VB.Frame Frame2 
  37.       Caption         =   "Speed"
  38.       Height          =   2295
  39.       Left            =   2040
  40.       TabIndex        =   3
  41.       Top             =   180
  42.       Width           =   1695
  43.       Begin VB.OptionButton OptionBaud 
  44.          Caption         =   "56000 Baud"
  45.          Height          =   255
  46.          Index           =   5
  47.          Left            =   120
  48.          TabIndex        =   10
  49.          Top             =   1860
  50.          Value           =   -1  'True
  51.          Width           =   1455
  52.       End
  53.       Begin VB.OptionButton OptionBaud 
  54.          Caption         =   "28800 Baud"
  55.          Height          =   255
  56.          Index           =   4
  57.          Left            =   120
  58.          TabIndex        =   9
  59.          Top             =   1560
  60.          Width           =   1455
  61.       End
  62.       Begin VB.OptionButton OptionBaud 
  63.          Caption         =   "14400 Baud"
  64.          Height          =   255
  65.          Index           =   3
  66.          Left            =   120
  67.          TabIndex        =   8
  68.          Top             =   1260
  69.          Width           =   1455
  70.       End
  71.       Begin VB.OptionButton OptionBaud 
  72.          Caption         =   "9600 Baud"
  73.          Height          =   255
  74.          Index           =   2
  75.          Left            =   120
  76.          TabIndex        =   7
  77.          Top             =   960
  78.          Width           =   1455
  79.       End
  80.       Begin VB.OptionButton OptionBaud 
  81.          Caption         =   "2400 Baud"
  82.          Height          =   255
  83.          Index           =   1
  84.          Left            =   120
  85.          TabIndex        =   6
  86.          Top             =   660
  87.          Width           =   1455
  88.       End
  89.       Begin VB.OptionButton OptionBaud 
  90.          Caption         =   "1200 Baud"
  91.          Height          =   255
  92.          Index           =   0
  93.          Left            =   120
  94.          TabIndex        =   4
  95.          Top             =   360
  96.          Width           =   1455
  97.       End
  98.    End
  99.    Begin VB.Frame Frame1 
  100.       Caption         =   "Device"
  101.       Height          =   1095
  102.       Left            =   240
  103.       TabIndex        =   0
  104.       Top             =   180
  105.       Width           =   1335
  106.       Begin VB.OptionButton OptionCom2 
  107.          Caption         =   "COM2"
  108.          Height          =   255
  109.          Left            =   120
  110.          TabIndex        =   2
  111.          Top             =   720
  112.          Width           =   975
  113.       End
  114.       Begin VB.OptionButton OptionCom1 
  115.          Caption         =   "COM1"
  116.          Height          =   255
  117.          Left            =   120
  118.          TabIndex        =   1
  119.          Top             =   360
  120.          Value           =   -1  'True
  121.          Width           =   975
  122.       End
  123.    End
  124. Attribute VB_Name = "Commcfg"
  125. Attribute VB_Creatable = False
  126. Attribute VB_Exposed = False
  127. Option Explicit
  128. Private CurrentBaudSetting&
  129. ' The port to use and configuration may have changed
  130. Private Sub CmdOk_Click()
  131.     Dim baudtouse&
  132.     Dim DeviceName$
  133.     ' A more general array approach would also be
  134.     ' easy to implement
  135.     Select Case CurrentBaudSetting
  136.         Case 0
  137.             baudtouse = 1200
  138.         Case 1
  139.             baudtouse = 2400
  140.         Case 2
  141.             baudtouse = 9600
  142.         Case 3
  143.             baudtouse = 14400
  144.         Case 4
  145.             baudtouse = 28800
  146.         Case 5
  147.             baudtouse = 56000
  148.     End Select
  149.     ' An approach similar to the baud rate one with indexed
  150.     ' option buttons would be better if you wanted to support
  151.     ' 4 or more comm ports
  152.     If OptionCom1.Value Then DeviceName = "COM1" Else DeviceName = "COM2"
  153.     If Not (Comm Is Nothing) Then
  154.         ' Comm is already valid. Have we changed ports?
  155.         If DeviceName <> Comm.DeviceName Then
  156.             ' We're changing device
  157.             ' Note that this also serves to close and
  158.             ' release the previous comm object
  159.             Set Comm = New dwComm
  160.             ' This demo doesn't use buffer sizes
  161.             Call Comm.OpenComm(DeviceName, CommDemo)
  162.         End If
  163.         ' If device is unchanged, SetCommState is all that is needed
  164.     Else
  165.         Set Comm = New dwComm
  166.         Call Comm.OpenComm(DeviceName, CommDemo)
  167.     End If
  168.     Comm.DCB.BaudRate = baudtouse
  169.     Comm.DCB.fNull = True
  170.     Comm.DCB.fErrorChar = True
  171.     Comm.DCB.ErrorChar = "~"
  172.     ' Perform any other DCB setting here
  173.     ' Now record the configuration changes
  174.     Call Comm.SetCommState
  175.     Unload Me
  176. End Sub
  177. Private Sub Form_Load()
  178.     If Comm Is Nothing Then
  179.         CurrentBaudSetting = 5
  180.     Else
  181.         Select Case (Comm.DCB.BaudRate)
  182.             Case 1200
  183.                     CurrentBaudSetting = 0
  184.             Case 2400
  185.                     CurrentBaudSetting = 1
  186.             Case 9600
  187.                     CurrentBaudSetting = 2
  188.             Case 14400
  189.                     CurrentBaudSetting = 3
  190.             Case 28800
  191.                     CurrentBaudSetting = 4
  192.             Case 56000
  193.                     CurrentBaudSetting = 5
  194.             Case Else
  195.                     CurrentBaudSetting = 5
  196.         End Select
  197.     End If
  198.     ' Set option button to current device
  199.     OptionBaud(CurrentBaudSetting).Value = True
  200. End Sub
  201. Private Sub OptionBaud_Click(Index As Integer)
  202.     CurrentBaudSetting = Index
  203. End Sub
  204.